<?php

namespace app\comm\service;

use app\comm\model\User;
use app\comm\model\Order;
use app\comm\model\UserWalletlog;
use app\comm\model\OrderDetails;

use app\comm\service\UserOrderService;
use app\comm\service\GoodsService;
use app\comm\service\UserService;
use app\comm\service\MyYcardService;

use think\Db;
use ServS\DbShowcollo;
/**
 * 支付服务层
 * 
 */
class PayService{

	//余额支付 按订单操作，不判断是否有库存
	public function paybyyue($data){
		$userservic = new UserService;
		$useorse 	= new  UserOrderService;
		$goodser 	= new GoodsService;
		$goodspe 	= $useorse ->orderbyid($data['orderid']);
		if($goodspe[0]['pay_status']!='0'){
			return 2;
		}

		if(array_key_exists("check", $data)){

			$result 		= $this->useycard($data);
			$final   		= $goodspe[0]['final_amount']-$result;
			$coupon_id  	=  implode(",",$data['check']);
			$coupon_price   = $result;
			$ji  =  $result/$final; 
			$this->dealycardbyorderdetail(['order_id'=>$data['orderid'],"money"=>$result,"ji"=>$ji]);
		}else{
			
			$final   	    = $goodspe[0]['final_amount'];
			$coupon_id      = '0';
			$coupon_price   =  0;
			$this->dealycardbyorderdetail(['order_id'=>$data['orderid'],"money"=>$final,"ji"=>0]);
		}
		$by  		=  ['uid'=>$data['uid'],'point'=>$final];
		$ref 		=  $this->userbuysomthing($by);	
						//用户减钱
		if($ref){
			$orderdata  = ['pay_status'=>1,"coupon_id"=>$coupon_id,"coupon_price"=>$coupon_price,
							"final_amount"=>$final,"pay_type"=>3,"pay_name"=>"余额支付"];
			$order 	 	= new Order;
			DbShowcollo::refresh($order,'id',$data['orderid'],$orderdata);
			DbShowcollo::refresh($order,'id',$data['orderid'],['order_status'=>1]);
			$dataaction =[ "uid" => $data['uid'], "order_id" =>$data['orderid'],"action_user"=>0,	
					 	   "order_status" =>1,"shipping_status"=>0,"pay_status"=>1,	
						   "status_desc" =>"余额支付","storeid"=>$goodspe[0]['store_id'],"addtime"=> time()];
			Db::table("ys_order_action")->insert($dataaction);
		}else{
			return 0;
		}
		if($ref){
			$use1   = User::where('uid', $data['uid'])->find()->toArray();
			$dau    = [ "uid" 	=>$data['uid'],"store_id"=>$goodspe[0]['store_id'],"order_id"=>$data['orderid'],
				        "uname" =>$data['nickname'], "amount"  =>$final ,"cur_amount"=>$use1['user_money']];
			$data   = $this->getwalletlog($dau);
			$this 	->insertwallog($data );
			return 1;
		}else{
			return 0;
		}
	}

	/**
	 * 用户购买商品
	 */
	public function userbuysomthing($array){	
		$use1 = User::where('uid', $array['uid'])->find()->toArray();
		if($use1['user_money']<$array['point']){
			return false;	//月不足就是3
		}	
		$use = User::where('uid', $array['uid'])->setInc('total_amount', $array['point']);//消费总额度
		$use = User::where('uid', $array['uid'])->setDec('user_money', $array['point']);
		return true;
	}

	/**
	 * 插入日志
	 */
	public function insertwallog($data){
		$userwalletlog  =  new UserWalletlog;
		DbShowcollo::add($userwalletlog,$data);
	}

	/**
	 * 优惠券使用
	 */
	public function useycard($data){
		$order = new Order;
		$myycardservice = new  MyYcardService;
		$mount = "";
		foreach ($data['check'] as $key => $value) {
		 	$resutl = $myycardservice->findycard($value);	
		 	$resu = $myycardservice->sentuse($value);
		 	$mount += $resutl['amount'];
		}
		return $mount;
	}

	/**
	 * 计算付费时订单详情的优惠价, 插入到订单详情表中
	 */
	public function dealycardbyorderdetail($array){
		$userOrderService = new UserOrderService;
		$resli = $userOrderService ->orderbyiddetaile($array['order_id']);
		foreach ($resli as $key => $value) {
			$value["total_price"] = round($value["total_price"]*(1-$array["ji"]),2);
			$value["spec_signal_price"] =  round($value["total_price"]/$value["goods_num"],5);
			$orderdetails = new OrderDetails;
			$datayu 	  = [ 'status'=>1,"signal_price"	=>	$value["total_price"],
							 "spec_signal_price" 	=>	$value["spec_signal_price"] ];
			DbShowcollo::refresh($orderdetails,'id',$value['did'],$datayu);
		}
	}

























	//消费log
	public function getwalletlog($data){
		$data= [
				 'uid'			=> $data['uid'],
				 'admin_id'		=> 0,
				 'store_id'		=> $data['store_id'],
				 'order_id'		=> $data['order_id'],
				 'order_sn'		=> $data['order_id'],
				 'uname'		=> $data['uname'],
				 'amount'		=> $data['amount'],
				 'cur_amount' 	=> $data['cur_amount'],
				 'sign'			=> 2,
				 'chargetype'	=> 1,
				 'charge_cn'	=>"购买商品"
				];
		return $data;	
	}
}